package com.hr.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.hr.dao.UserDao;
import com.hr.model.UserModel;
import com.hr.util.PasswordEncoder;
import com.hr.util.SqlUtil;
@Repository
public class UserDaoImpl implements UserDao{
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Override
	public List<UserModel> find(String loginName,String passwrod) {
		String sql = "select * from \"user\" where login_name=? and login_password=? and valid='1'";
		List<UserModel> result = jdbcTemplate.query(sql, new BeanPropertyRowMapper<UserModel>(UserModel.class),loginName,passwrod);
		return result;
	}
	@Override
	public List<UserModel> findAll() {
		String sql = "select * from \"user\" where valid='1'";
		List<UserModel> userList = jdbcTemplate.query(sql,new BeanPropertyRowMapper<UserModel>(UserModel.class));
		return userList;
	}
	@Override
	public UserModel queryByPrimaryKey(String user_id) {
		String sql = "select * from \"user\" where user_id=? and valid='1'";
		UserModel userModel = jdbcTemplate.queryForObject(sql, new Object[]{user_id}, new BeanPropertyRowMapper<UserModel>(UserModel.class));
		return userModel;
	}
	@Override
	public boolean insert(UserModel userModel) {
		userModel.setUser_id(getNewCode());
		userModel.setLogin_password(PasswordEncoder.MD5(userModel.getLogin_name()));
		Map<String,Object> map = SqlUtil.generateInsertModelInfo(userModel);
		String sql = "insert into \"user\" ";
		sql+=" ("+map.get("fields")+") ";
		sql+=" values("+map.get("preValues")+")";
		
		jdbcTemplate.update(sql, (Object[])map.get("fieldParams"));
		return true;
	}
	@Override
	public boolean update(UserModel userModel) {
		Map<String,Object> map = SqlUtil.generateUpdateModelInfo(userModel,"user_id","login_password");
		String sql = "update \"user\" set " + map.get("fields")
				+ " where user_id=?";
		//获取映射值
		Object[] params = (Object[]) map.get("fieldParams");
		Object[] condition = java.util.Arrays.copyOf(params, params.length+1);
		condition[condition.length-1] = userModel.getUser_id();
		jdbcTemplate.update(sql, condition);
		return true;
	}
	/**
	 * 自定生成部门编号
	 * @return
	 */
	private String getNewCode(){
		String code="001";
		code = jdbcTemplate.queryForObject("select max(user_id) from \"user\"", String.class);
		if(code==null)
			return "001";
		int num = Integer.parseInt(code);
		num++;
		if(num<10)
			code="00"+num;
		else if(num<100)
			code="0"+num;
		else
			code = ""+num;
		return code;
	}
	@Override
	public boolean delete(String user_id) {
		String sql = "update  \"user\" set valid='0' where user_id=?";
		jdbcTemplate.update(sql, user_id);
		return true;
	}
	@Override
	public boolean updatePassword(String userId, String newPassword) {
		String sql = "update \"user\" set login_password=? where user_id=?";
		int count = jdbcTemplate.update(sql, newPassword,userId);
		return count==1?true:false;
	}
}
